<?php

define('HOST', 'localhost');
define('USER', 'uapv9903506');
define('PASS', 'brlv7p');
define('DB', 'uapv9903506');

class dbconnection {

    private $link, $error;
    private static $instance = null;

    public static function getInstance() {
        if (self::$instance == null)
            self::$instance = new dbconnection();
        return self::$instance;
    }

    public function __construct() {
        $this->link = null;
        $this->error = null;
        try {
//            $this->link = new PDO("psql:host=" . HOST . ";dbname=" . DB . ";user=" . USER . ";password=" . PASS);
            $this->link = new PDO("mysql:host=" . HOST . ";dbname=" . DB, USER, PASS);
        } catch (PDOException $e) {
            $this->error = $e->getMessage();
        }
    }

    public function getLastInsertId($att) {
        return $this->link->lastInsertId($att . "_id_seq");
    }

    public function doExec($sql) {
        $prepared = $this->link->prepare($sql);
        return $prepared->execute();
    }

    public function doQuery($sql) {
        $prepared = $this->link->prepare($sql);
        $prepared->execute();
        $res = $prepared->fetchAll(PDO::FETCH_ASSOC);

        return $res;
    }

    public function doQueryObject($sql) {
        $prepared = $this->link->prepare($sql);
        $prepared->execute();
        $res = $prepared->fetchAll(PDO::FETCH_OBJ);

        return $res;
    }

    public function __destruct() {
        $this->link = null;
    }

}
